package com.photoeditor.demo.model.store.module;

import android.text.TextUtils;

import com.android.gpuimage.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Created by mutour on 2017/5/9.
 */

public class StoreParseUtil {

    public static String joinUrl(String... urls) {
        if (urls == null || urls.length == 0) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (String url : urls) {
            boolean hasPrefix = TextUtils.isEmpty(url) ? false : url.codePointAt(0) == '/';
            if (builder.length() > 0 && builder.codePointAt(builder.length() - 1) == '/') {
                if (hasPrefix) {
                    builder.append(url, 1, url.length());
                } else {
                    builder.append(url);
                }
            } else {
                if (hasPrefix) {
                    builder.append(url);
                } else {
                    if (builder.length() > 0) {
                        builder.append('/');
                    }
                    builder.append(url);
                }
            }
        }

        return builder.toString();
    }

    public static String compress(String str){
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String outStr = null;
        try {
            outStr = Base64.encodeBytes(out.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outStr;
    }

    public static String decompress(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        GZIPInputStream gis = null;
        String outStr = "";
        try {
            gis = new GZIPInputStream(new ByteArrayInputStream(Base64.decode(str)));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[256];
            int n;
            while ((n = gis.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            outStr = new String(out.toByteArray());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(gis != null) {
                try {
                    gis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return outStr;
    }
}
